import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_09_game/config/app_color.dart';
import 'package:flutter_09_game/generated/assets.dart';
import 'package:flutter_09_game/widgets/image/auto_image.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

///  左右按钮标题栏
class CommonAppBar extends StatelessWidget implements PreferredSizeWidget {
  const CommonAppBar({
    super.key,
    required this.title,
    this.titleColor,
    this.onLeftClick,
    this.onRightClick,
    this.centerTitle,
    this.showRight = false,
    this.showLeft = true,
    this.actions,
    this.leadingImage,
    this.leadingImageColor,
    this.backgroundColor = AppColor.navBgColor,
    this.focusNode,
    this.leadingWidth,
    this.titleWidget,
    this.systemOverlayStyle,
    this.titleSpacing,
    this.leftWidget,
    this.showBack = true,
    // 141416
  });

  final Function()? onLeftClick;
  final Function()? onRightClick;
  final String title;
  final Color? titleColor;
  final bool? centerTitle;
  final bool? showRight;
  final bool? showLeft;
  final Widget? leftWidget;
  final List<Widget>? actions;
  final String? leadingImage;
  final Color? leadingImageColor;
  final Color backgroundColor;
  final Widget? titleWidget;
  final FocusNode? focusNode;
  final SystemUiOverlayStyle? systemOverlayStyle;
  final double? titleSpacing;
  final double? leadingWidth;
  final bool showBack;

  @override
  Widget build(BuildContext context) {
    return _buildAppBar();
  }

  Widget _buildAppBar() {
    return AppBar(
      scrolledUnderElevation: 0,
      //滑动渐变
      title: titleWidget,
      centerTitle: centerTitle ?? true,
      leading: showLeft == true
          ? (leftWidget ?? _buildLeftButton())
          : const SizedBox(),
      automaticallyImplyLeading: showLeft == false,
      leadingWidth: leadingWidth ?? 152.w,
      titleSpacing: titleSpacing ?? 16.w,
      actions: actions,
      backgroundColor: backgroundColor,
      toolbarHeight: kToolbarHeight,
      systemOverlayStyle: systemOverlayStyle,

    );
    // SystemChrome.setSystemUIOverlayStyle( const SystemUiOverlayStyle(
    //   systemNavigationBarColor: AppColor.navBarBackground,
    //   statusBarColor: Colors.transparent,
    //   statusBarIconBrightness: Brightness.light,
    // ));
  }

  _buildLeftButton() {
    return GestureDetector(
        onTap: () {
          if (onLeftClick != null) {
            onLeftClick?.call();
          } else {
            if (showBack) {
              Get.back();
            }
          }
          FocusManager.instance.primaryFocus?.unfocus();
        },
        behavior: HitTestBehavior.opaque,
        child: Container(
            height: 52,
            width: 150.w,
            // alignment: Alignment.centerLeft,
            // padding: EdgeInsets.only(left: 6.w),
            decoration: const BoxDecoration(
                // color: Colors.red,
                image: DecorationImage(
                    image: AssetImage(
                      Assets.commonBackBg,
                    ),
                    fit: BoxFit.cover)),
            child: Row(
              children: [
                8.horizontalSpace,
                Visibility(
                    visible: showBack,
                    child: AutoImage(
                      Assets.commonBack,
                      width: 16.w,
                    ).marginOnly(right: 4.w)),
                Text(
                  title,
                  style: TextStyle(
                      fontSize: 14.sp,
                      fontWeight: FontWeight.w600,
                      color: AppColor.textPrimary.withOpacity(0.94)),
                )
              ],
            )));
  }

  @override
  Size get preferredSize => Size.fromHeight(44.w);
}
